package com.wzcl.app.model.common;

import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 验证码封装类
 */
@Data
public class ValidateCode implements Serializable {
    private static final long serialVersionUID = 1L;

    private String code;
    private LocalDateTime expireTime;

    public ValidateCode(String code, int validityMinutes) {
        this.code = code;
        this.expireTime = LocalDateTime.now().plusMinutes(validityMinutes);
    }

    public boolean isExpire() {
        return LocalDateTime.now().isAfter(expireTime);
    }

    public static ValidateCode generate(int validityMinutes) {
        String code = String.valueOf(Math.round(Math.random()*899999+100000));
        return new ValidateCode(code, validityMinutes);
    }
}
